package com.gljx.web.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gljx.util.Axamine;
import com.gljx.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.util.GetFid;
import com.gljx.web.Entity.ChargeClazzPremium;
import com.gljx.web.Entity.ChargeClazzWillmigerl;
import com.gljx.web.Entity.ChargeClazzWillmigerlEntry;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.ChargeClazzWillmigerlEntryMapper;
import com.gljx.web.dao.ChargeClazzWillmigerlMapper;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.service.ChargeClazzWillmigerlService;

@Service
@Transactional
public class ChargeClazzWillmigerlServiceImpl extends CommonDao implements ChargeClazzWillmigerlService{

	@Autowired
	private ChargeClazzWillmigerlEntryMapper chargeClazzWillmigerlEntryMapper;
	
	@Autowired
	private ChargeClazzWillmigerlMapper chargeClazzWillmigerlMapper;
	
	
	/**
	 *取分页数据    dmz 20160921
	 *@param pindex 页码
	 *@param psql SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
	 *@param psize 每页记录数
	 * */
	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		  Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Pindex", pindex);
	       paramMap.put("Psql", psql);
	       paramMap.put("Psize", psize);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.ChargeClazzWillmigerlMapper.pagemap", paramMap, null);
	       return  paramMap;
	}

	/**
	 * 取记录总数      dmz 20160921
	 * @param psql   SQL语句
	 * @return
	 */
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Psql", psql);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       this.getSqlSession().select("com.gljx.web.dao.ChargeClazzWillmigerlMapper.pagecount", paramMap, null);
	       return paramMap;
	}

	/**
	 * 根据条件查询子类信息
	 */
	@Override
	public List<ChargeClazzWillmigerlEntry> queryChargeClazzWillmigerlEntry(String fparentId, String studentName,
			String cfidcardno, String cfmobile) {
		// TODO Auto-generated method stub
		 Map<String, Object> paramMap = new HashMap<String, Object>();
	     paramMap.put("v_fparentid", fparentId);
	     paramMap.put("v_studentName", studentName);
	     paramMap.put("v_cfidcardno", cfidcardno);
	     paramMap.put("v_cfmobile", cfmobile);
	     paramMap.put("v_cursor", oracle.jdbc.OracleTypes.CURSOR);
	     this.getSqlSession().select("com.gljx.web.dao.ChargeClazzWillmigerlEntryMapper.selectChargeClazzWillmigerlEntry", paramMap, null);
	     @SuppressWarnings("unchecked")
		 List<ChargeClazzWillmigerlEntry> chargeClazzWillmigerlEntrys = (List<ChargeClazzWillmigerlEntry>) paramMap.get("v_cursor");
		 return chargeClazzWillmigerlEntrys;
	}

	/**
	 * 添加或修改
	 */
	@Override
	public void editChargeClazzWillmigerl(ChargeClazzWillmigerl chargeClazzWillmigerl, String[] fseq,
			String[] cfstudentno, String[] cfstudentid, String[] cfidcardno, String[] cfsex, String[] cfmobile,
			String[] cfrxrq, String[] cfcurrentclassid, String[] cfamount, String[] cfshje, String[] cfreturninclassid,
			String[] cfzrbjamount, String[] cfsfje, String[] cfremark, User user, String time) {
		// TODO Auto-generated method stub
		if("".equals(chargeClazzWillmigerl.getFid()) || chargeClazzWillmigerl.getFid() == null){
			//创建人id
			chargeClazzWillmigerl.setFcreatorid(user.getFid());
			//利用UUID生成fid
			chargeClazzWillmigerl.setFid(GetFid.UUIDIntoFid());
			//当前时间
			chargeClazzWillmigerl.setFcreatetime(time);
			//状态
            chargeClazzWillmigerl.setCfbillstate(3L);
            //添加
			chargeClazzWillmigerlMapper.insertSelective(chargeClazzWillmigerl);
		}else{
			//修改时间时间
			chargeClazzWillmigerl.setFlastupdatetime(time);
			//修改人id
			chargeClazzWillmigerl.setFlastupdateuserid(user.getFid());
			//修改
			chargeClazzWillmigerlMapper.updateByPrimaryKeySelective(chargeClazzWillmigerl);
			//先删除子类信息
			chargeClazzWillmigerlEntryMapper.deleteByFparentId(chargeClazzWillmigerl.getFid());
		}
		//添加子类
		List<ChargeClazzWillmigerlEntry> chargeClazzWillmigerlEntrys = new ArrayList<>();
		if(fseq == null || fseq.length == 0){
					
		}else{
			for (int i = 0; i < fseq.length; i++) {
				ChargeClazzWillmigerlEntry chargeClazzWillmigerlEntry = new ChargeClazzWillmigerlEntry();
				chargeClazzWillmigerlEntry.setCfstudentno(cfstudentno[i]);
				chargeClazzWillmigerlEntry.setCfstudentid(cfstudentid[i]);
				chargeClazzWillmigerlEntry.setCfidcardno(cfidcardno[i]);
				if("".equals(cfsex[i])){
					
				}else{
					chargeClazzWillmigerlEntry.setCfsex(Long.parseLong(cfsex[i]));
				}
				chargeClazzWillmigerlEntry.setCfmobile(cfmobile[i]);
				chargeClazzWillmigerlEntry.setCfrxrq(cfrxrq[i]);
				chargeClazzWillmigerlEntry.setCfcurrentclassid(cfcurrentclassid[i]);
				if("".equals(cfamount[i])){
					
				}else{
					chargeClazzWillmigerlEntry.setCfamount(new BigDecimal(cfamount[i]));
				}
				if("".equals(cfshje[i])){
					
				}else{
					chargeClazzWillmigerlEntry.setCfshje(new BigDecimal(cfshje[i]));
				}
				chargeClazzWillmigerlEntry.setCfreturninclassid(cfreturninclassid[i]);
				if("".equals(cfzrbjamount[i])){
					
				}else{
					chargeClazzWillmigerlEntry.setCfzrbjamount(new BigDecimal(cfzrbjamount[i]));
				}
				if("".equals(cfsfje[i])){
					
				}else{
					chargeClazzWillmigerlEntry.setCfsfje(new BigDecimal(cfsfje[i]));
				}
				chargeClazzWillmigerlEntry.setCfremark(cfremark[i]);
				chargeClazzWillmigerlEntry.setFseq((long) i+1);
				chargeClazzWillmigerlEntry.setFid(GetFid.UUIDIntoFid());
				chargeClazzWillmigerlEntry.setFparentid(chargeClazzWillmigerl.getFid());
				chargeClazzWillmigerlEntrys.add(chargeClazzWillmigerlEntry);
			}
			chargeClazzWillmigerlEntryMapper.insertChargeClazzWillmigerlEntry(chargeClazzWillmigerlEntrys);
		}
	}

	/**
	 * 根据fid查询
	 */
	@Override
	public ChargeClazzWillmigerl queryChargeClazzWillmigerlByFid(String fid) {
		// TODO Auto-generated method stub
		return chargeClazzWillmigerlMapper.selectChargeClazzWillmigerlByFid(fid);
	}

	/**
	 * 根据fid删除
	 */
	@Override
	public void deleteChargeClazzWillmigerlByFid(String[] deleteId, String fid) {
		// TODO Auto-generated method stub
		if("".equals(fid) || fid == null){
			//先删除子类信息
			chargeClazzWillmigerlEntryMapper.deleteByArray(deleteId);
			//在删除父类信息
			chargeClazzWillmigerlMapper.deleteByArray(deleteId);
		}else{
			//先删除子类信息
			chargeClazzWillmigerlEntryMapper.deleteByFparentId(fid);
			//在删除父类信息
			chargeClazzWillmigerlMapper.deleteByPrimaryKey(fid);
		}
	}

	/**
	 * 审核，反审核
	 */
	@Override
    public int auditChargeClazzWillmigerl(String fid, String type, String fhandlerId, String message, String state, String fnumber) {
        // TODO Auto-generated method stub
        ChargeClazzWillmigerl chargeClazzWillmigerl = new ChargeClazzWillmigerl();
        //fid
        chargeClazzWillmigerl.setFid(fid);
        Axamine axamine = new Axamine();
        if ("1".equals(type)) {
            ServerResponse serverResponse = axamine.auditProcess(fnumber, state, message);
            if (serverResponse.getStatus() == 1) {
                chargeClazzWillmigerl.setCfbillstate(5L);
                //审核人
                chargeClazzWillmigerl.setFauditorid(fhandlerId);
                //修改人
                chargeClazzWillmigerl.setFlastupdateuserid(fhandlerId);
                //结束
                return chargeClazzWillmigerlMapper.updateByPrimaryKeySelective(chargeClazzWillmigerl);
            } else if (serverResponse.getStatus() == 2) {
                //推进
                if (!"1".equals(state)) {
                    chargeClazzWillmigerl.setCfbillstate(3L);
                    chargeClazzWillmigerlMapper.updateByPrimaryKeySelective(chargeClazzWillmigerl);
                }
                return 2;
            } else {
                //错误
                return 0;
            }
        }else{
            axamine.againstAuditProcess(fnumber, message);
            chargeClazzWillmigerl.setCfbillstate(3L);
            return chargeClazzWillmigerlMapper.updateByPrimaryKeySelective(chargeClazzWillmigerl);
        }

    }

    @Override
    public int modifyChargeClazzWillmigerl(ChargeClazzWillmigerl chargeClazzWillmigerl) {
        return chargeClazzWillmigerlMapper.updateByPrimaryKeySelective(chargeClazzWillmigerl);
    }

	@Override
	public List<ChargeClazzWillmigerlEntry> selectChargeClazzWillmigerl(String fcompanyid, String studentName, String cfidcardno, String cfmobile, String beginTime, String endTime) {
		Map<String, String> map = new HashMap<>();
		map.put("studentName", studentName);
		map.put("cfidcardno", cfidcardno);
		map.put("cfmobile", cfmobile);
		map.put("beginTime", beginTime);
		map.put("endTime", endTime);
		map.put("fcompanyid", fcompanyid);
		return chargeClazzWillmigerlEntryMapper.selectChargeClazzWillmigerl(map);
	}
}
